<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>作业：短信验证码；显示时间</p>
    <h1 id="time_1"></h1>
    <button id="btn1">获取验证码</button>
    <script>
        var btn1 = document.getElementById('btn1');
        var time1 = 10;
        var timer = null;
        btn1.onclick = function() {
            if(time1 == 0) {
                // 等于0时 再次点击 可以重新开始
                time1 = 10;
                clearInterval(timer)
            }
            // 省略了校验的过程
            timer = setInterval(function(){
                if(time1>0) {
                    btn1.innerHTML = '获取验证码('+time1--+')';
                }else {
                    btn1.innerHTML = '重新获取';
                }
            }, 1000);
        }

        function showTime () {
            // 获取元素
            var time_1 = document.getElementById('time_1');
            setInterval(function() {
                var t = new Date()
                time_1.innerHTML = t.toLocaleTimeString()
            }, 1000);
        }
        showTime()
    </script>
</body>
</html>